$(document).ready(
				function() {
					//富文本编辑器
					var ue=UE.getEditor('pic');
					
					var totalCount = $("#totalCount").val();
					var pSize=$("#pageSize").val();;
					var pageIndex = $("#pageIndex").val();
					var pageCount = $("#pageCount").val();
					

					$("#prDetails").on('hidden.bs.modal', function() {
						$(this).removeData("bs.modal");
					});

					$("#optBtn")
							.delegate(
									'#saveBtn','click',
									function() {
										
										$("#products").attr("method","post");
										
										var id = $("#prid").val();
									
											if (id =="") {
												$("#products").attr("action","save.do");
											} else {
												$("#products").attr("action","update.do");
											}
											$("#products").submit();
										
									});

					$('#pagination2').jqPaginator(
									{
										 totalPages: pageCount,
										    visiblePages: 10,
										    currentPage: 1,
										
										    prev: '<li class="prev"><a href="javascript:void(0);">上一页</a></li>',
										    page: '<li class="page"><a href="javascript:void(0);">{{page}}</a></li>',
										    next: '<li class="next"><a href="javascript:void(0);">下一页</a></li>',
										onPageChange : function(num, type) {
											$("#pageIndex").val(num);
											var queryUrl = "queryByPage.do";
											$.ajax({
														url : queryUrl,
														dataType : 'json',
														data : {
															pageIndex :num,
															pageSize : pSize
														},
														cache : false,
														success : function(data) {

															
															$("#prList tr:not(:first)").remove();
															var results = data.pageResult.resultList;
															for (i = 0; i < results.length; i++) {
																var pr = results[i];
																var rowContent = "<tr>"
																	+ "<td><input name='id' id='prid' class='checkbox' type='checkbox' value='"
																	+ pr.id
																	+ "'/></td>"
																	+ "<td>"
																	+ pr.id
																	+ "</td>"
																	+ "<td>"
																	+ pr.name
																	+ "</td>"
																	/*+ "<td>"
																	+ pr.pic
																	+ "</td>"*/
																	+ "<td>"
																	+ pr.classification.name
																	+ "</td>"
																rowContent = rowContent
																		+ "<td>" 
																		+ "<button name='editBtn' objId='"
																		+ pr.id
																		+ "' class='btn btn-sm-block'>修改</button>" 
																		+ "</td>" 
																		+ "</tr>";
																$("#prList")
																		.append(
																				rowContent);
															}

														},
														error : function(html) {
															return;
														}
													});
										}
									});

					/**
					 * 全选当前页记录
					 */
					$("#prList").delegate('#selectAll', 'click', function() {
						var isSelectAll = $(this).prop('checked');
						$("input[name='id']").each(function() {
							$(this).prop("checked", isSelectAll);
						});
					});

					/**
					 * 修改按钮事件
					 */

					$("#prList").delegate('button', 'click', function() {
						$("#checks input").removeAttr("readonly");
						$("#checks textarea").removeAttr("readonly");
						$("#checks select").removeAttr("disabled");
						
						var prid = $(this).attr("objId");
						var editUrl = "show.do";
						$.ajax({

							url : editUrl,
							dataType : 'json',
							data : {
								id : prid
							},
							cache : false,

							success : function(data) {
								
								loadGI(data);
							},
							error : function(html) {
								alert("Load Data failly");
								refresh();
							}
						});

					});
					
					/**
					 * 查看详情
					 */
					$("#prList").delegate('#checkBtn', 'click', function() {
						$("#checks input").attr({ readonly: 'true' });
						$("#checks textarea").attr({ readonly: 'true' });
						$("#checks select").attr("disabled","disabled");
					});

					
					/**
					  删除按钮事件
					 */
					$("#tools").delegate(
						'#deleteBtn',
						'click',
						function() {
							delChk = $('input[name="id"]:checked');

							if (delChk.length > 0) {
								if (confirm("Are you sure delete the record?")) {

									var ids = "";
									var delUrl = "delete.do";
                                    var idArr=new Array();
                                   
                                    
                                    for (var i = 0; i < delChk.length; i++) {
                                    	idArr.push(delChk.get(i).value);
                               
								}
									
									$.ajax({
												url : delUrl,
												type: "POST",
												data : {
													ids : idArr.toString()
												},
												cache : false,

												success : function(
														html) {
													alert("Delete successfully");
													refresh();
												},
												error : function(
														html) {
													alert("Delete failly");
													refresh();
												}
											});
								} else {
									return;
								}
							} else {
								alert("Please select the record to delete");
								return;
							}
						});
					
					
					
					/**
					 * 添加
					 */
					$("#tools").delegate('#addproducts', 'click', function() {
						
						$("#prDetails input").each(function() {
							$(this).prop("value", null);
							$(this).prop("readonly", false);

						});

						$("#prDetails textarea").each(function() {
							$(this).prop("value", null);

						});
						$("#pic").each(function() {
							ue.setContent("");
							
						});
						
						$("#prDetails select").each(function() {
							$(this).prop("value", 0);
							
						});
						
						$("#prDetails").modal('show');
						$("#pic").modal('show');

					});
				


/**
 * 修改页面上的默认值
 */
function loadGI(products) {
	$("#prid").val(products.id);
	$("#name").val(products.name);
//	$("#pic").val(products.pic);
	$("#classification").val(products.classification.id);
	$("#classId").val(products.classification.id);
	
	//富文本编辑器
	ue.setContent(products.pic_str);
	
	$("#prDetails").modal('show');
}

function vaildate(form) {
	return true;
}
/**
 * 刷新页面
 * */
function refresh() {
	window.location.reload();
}
				});
